# encoding: utf-8

class Report::TransferReportController < ApplicationController
  before_filter :authenticate_user!
  def show
     if (user_signed_in? && ([4].include?(current_user.role)))
      @title_view = 'Reporte de Traslados'
      @heritages = CulturalHeritage::CulturalHeritage.all
      @origins = CulturalHeritage::CulturalInstitution.all
      @destinies = CulturalHeritage::CulturalInstitution.all
     else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  def show_pdf
    if (user_signed_in? && ([4].include?(current_user.role)))
    @title_view = 'Reporte de Traslados'
    @heritage_name = params[:heritage_name]
    @origin_place = params[:origin_place]
    @destiny_place = params[:destiny_place]
    @transfer_state = params[:transfer_state]
#    @cultural_heritage_transfers = CulturalHeritage::Transfer.all
#    @cultural_heritage_x_transfer = @cultural_heritage_transfers.list_heritage_x_transfer

#    @cultural_heritage_transfers =
#            CulturalHeritage::Transfer.find(
#              :all,
#              :select => 'DISTINCT `cultural_heritage_transfers` . *',
#              :joins=>[
#                [:list_heritage_x_transfer =>
#                  :obj_cultural_heritage],
#                :obj_cultural_institution_origin,
#                :obj_cultural_institution_destiny,
#                :obj_responsable
#              ],
#              :conditions=> [
#                "cultural_heritage_cultural_heritages.id LIKE ? AND " +
#                "cultural_heritage_transfers.origin LIKE ? AND " +
#                "cultural_heritage_transfers.destiny LIKE ? ",
#                @heritage_name, @origin_place, @destiny_place
#              ]
#            )

    if @transfer_state.blank? and @origin_place.blank? and @destiny_place.blank? and @heritage_name.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ])
    end


    
      if ! @transfer_state.blank? and ! @origin_place.blank? and ! @destiny_place.blank? and ! @heritage_name.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? AND cultural_heritage_transfers.origin = ? AND cultural_heritage_transfers.destiny = ? AND cultural_heritage_transfers.state = ?",@heritage_name,@origin_place,@destiny_place,@transfer_state])
      end

      if ! @transfer_state.blank? and @origin_place.blank? and @destiny_place.blank? and @heritage_name.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_transfers.state = ?",@transfer_state])
      end
      if ! @transfer_state.blank? and ! @heritage_name.blank? and ! @destiny_place.blank? and @origin_place.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.destiny = ? and cultural_heritage_transfers.state = ? ",@heritage_name,@destiny_place,@transfer_state])
      end
      if ! @transfer_state.blank? and ! @heritage_name.blank? and ! @origin_place.blank? and @destiny_place.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.origin = ? and cultural_heritage_transfers.state = ? ",@heritage_name,@origin_place,@transfer_state])
      end

      if ! @transfer_state.blank? and ! @heritage_name.blank? and @origin_place.blank? and @destiny_place.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.state = ? ",@heritage_name,@transfer_state])
      end

      if ! @transfer_state.blank? and ! @origin_place.blank? and @destiny_place.blank? and @heritage_name.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_transfers.state = ? and cultural_heritage_transfers.origin = ? ",@transfer_state,@origin_place])
      end

      if ! @transfer_state.blank? and ! @destiny_place.blank? and @origin_place.blank? and @heritage_name.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_transfers.state = ? and cultural_heritage_transfers.destiny = ? ",@transfer_state,@destiny_place])
      end
#    end
#    else
      if @transfer_state.blank? and ! @origin_place.blank? and ! @destiny_place.blank? and ! @heritage_name.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],:conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.origin = ? and cultural_heritage_transfers.destiny = ?",@heritage_name,@origin_place,@destiny_place])
      end
#    end

    
      if ! @origin_place.blank? and @transfer_state.blank? and @destiny_place.blank? and @heritage_name.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_transfers.origin = ? ",@origin_place])
      end
      if ! @origin_place.blank? and ! @heritage_name.blank? and ! @destiny_place.blank? and @transfer_state.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.destiny = ? and cultural_heritage_transfers.origin = ? ",@heritage_name,@destiny_place,@origin_place])
      end
      if ! @origin_place.blank? and ! @heritage_name.blank? and ! @transfer_state.blank? and @destiny_place.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.origin = ? and cultural_heritage_transfers.state = ? ",@heritage_name,@origin_place,@transfer_state])
      end
      if ! @origin_place.blank? and ! @heritage_name.blank? and @destiny_place.blank? and @transfer_state.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.origin = ? ",@heritage_name,@origin_place])
      end
      if ! @origin_place.blank? and ! @destiny_place.blank? and @transfer_state.blank? and @heritage_name.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_transfers.origin = ? and cultural_heritage_transfers.destiny = ? ",@origin_place,@destiny_place])
      end
      if ! @origin_place.blank? and ! @transfer_state.blank? and @heritage_name.blank? and @destiny_place.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_transfers.origin = ? and cultural_heritage_transfers.state = ? ",@origin_place,@transfer_state])
      end
#    end
#    else
      if  @origin_place.blank? and ! @transfer_state.blank? and ! @destiny_place.blank? and ! @heritage_name.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.state = ? and cultural_heritage_transfers.destiny = ?",@heritage_name,@transfer_state,@destiny_place])
      end
#    end

    
      if ! @destiny_place.blank? and @origin_place.blank? and @transfer_state.blank? and @heritage_name.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_transfers.destiny = ? ",@destiny_place])
      end
      if ! @destiny_place.blank? and ! @heritage_name.blank? and ! @origin_place.blank? and @transfer_state.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.origin = ? and cultural_heritage_transfers.destiny = ? ",@heritage_name,@origin_place,@destiny_place])
      end
      if ! @destiny_place.blank? and ! @heritage_name.blank? and ! @transfer_state.blank? and @origin_place.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.destiny = ? and cultural_heritage_transfers.state = ? ",@heritage_name,@destiny_place,@transfer_state])
      end
      if ! @destiny_place.blank? and ! @heritage_name.blank? and @origin_place.blank? and @transfer_state.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.destiny = ? ",@heritage_name,@destiny_place])
      end
      if ! @destiny_place.blank? and ! @origin_place.blank? and @heritage_name.blank? and @transfer_state.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_transfers.destiny = ? and cultural_heritage_transfers.origin = ? ",@destiny_place,@origin_place])
      end
      if ! @destiny_place.blank? and ! @transfer_state.blank? and @origin_place.blank? and @heritage_name.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_transfers.state = ? and cultural_heritage_transfers.destiny = ? ",@transfer_state,@destiny_place])
      end
#    end
#    else
      if @destiny_place.blank? and ! @transfer_state.blank? and ! @origin_place.blank? and ! @heritage_name.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.state = ? and cultural_heritage_transfers.origin = ?",@heritage_name,@transfer_state,@origin_place])
      end
#    end

    
     if ! @heritage_name.blank? and @origin_place.blank? and @destiny_place.blank? and @transfer_state.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ?",@heritage_name])
      end
      if ! @heritage_name.blank? and ! @origin_place.blank? and ! @destiny_place.blank? and @transfer_state.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.origin = ? and cultural_heritage_transfers.destiny = ? ",@heritage_name,@origin_place,@destiny_place])
      end
      if ! @heritage_name.blank? and ! @transfer_state.blank? and ! @origin_place.blank? and @destiny_place.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.state = ? and cultural_heritage_transfers.origin = ? ",@heritage_name,@origin_place,@transfer_state])
      end
     if ! @heritage_name.blank? and ! @transfer_state.blank? and @origin_place.blank? and @destiny_place.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.state = ? ",@heritage_name,@transfer_state])
      end
      if ! @heritage_name.blank? and ! @origin_place.blank? and @destiny_place.blank? and @transfer_state.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.origin = ? ",@heritage_name,@origin_place])
      end
      if ! @heritage_name.blank? and ! @destiny_place.blank? and @origin_place.blank? and @transfer_state.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_my_heritage_x_transfers.heritage = ? and cultural_heritage_transfers.destiny = ? ",@heritage_name,@destiny_place])
      end
#    end
#    else
      if @heritage_name.blank? and ! @transfer_state.blank? and ! @origin_place.blank? and ! @destiny_place.blank?
        @cultural_heritage_transfers = CulturalHeritage::Transfer.find(:all,:select => 'DISTINCT `cultural_heritage_transfers` . *',
              :joins=>[
                [:list_heritage_x_transfer =>
                  :obj_cultural_heritage],
                :obj_cultural_institution_origin,
                :obj_cultural_institution_destiny,
              ],
              :conditions=>["cultural_heritage_transfers.state = ? and cultural_heritage_transfers.destiny = ? and cultural_heritage_transfers.origin = ?",@transfer_state,@destiny_place,@origin_place])
      end
#    end


    pdf = Prawn::Document.new

    pdf.image "#{RAILS_ROOT}/public/images/pukara_logo.png", :width => 80, :at => [0, 715]
    pdf.text_box "Sistema de información cultural pukara",
      :at => [100, 715],
      :width => 430
    #    Titulo del reporte
    pdf.text_box "Reporte de Traslados",
      :at => [100, 690],
      :width => 430,
      :style => :bold,
      :size => 20
    #    Cuerpo   
    
    pdf.move_down 150
    
    arr = Array.new
    
    #Colocando headers
    arr[0] = Array.new
    arr[0][0] = "Cód."
    arr[0][1] = "Patrimonio Cultural"
    arr[0][2] = "Origen"
    arr[0][3] = "Destino"
    arr[0][4] = "Fecha de modificación"
    arr[0][5] = "Estado"

    j = 1

    @cultural_heritage_transfers.each do |transfer|
      transfer.list_heritage_x_transfer.each do |heritage_x_transfer|


        if !@heritage_name.blank?
          if heritage_x_transfer.obj_cultural_heritage.id == @heritage_name.to_i
            arr[j] = Array.new
            arr[j][0] = transfer.id
            arr[j][1] = heritage_x_transfer.obj_cultural_heritage.name#transfer.list_heritage_x_transfer.length
            arr[j][2] = transfer.obj_cultural_institution_origin.name
            arr[j][3] = transfer.obj_cultural_institution_destiny.name
            arr[j][4] = transfer.updated_at.strftime("%d-%m-%Y").to_s
            arr[j][5] = transfer.str_state
            j = j + 1
          end
        else
          arr[j] = Array.new
          arr[j][0] = transfer.id
          arr[j][1] = heritage_x_transfer.obj_cultural_heritage.name#transfer.list_heritage_x_transfer.length
          arr[j][2] = transfer.obj_cultural_institution_origin.name
          arr[j][3] = transfer.obj_cultural_institution_destiny.name
          arr[j][4] = transfer.updated_at.strftime("%d-%m-%Y").to_s
          arr[j][5] = transfer.str_state
          j = j + 1

        end
      end
    end

    pdf.table arr, :column_widths => [30, 102, 102, 102, 102, 102], :row_colors => ["F0F0F0", "FFFFFF"], :header => true do
      row(0).border_width = 2
      row(0).font_style = :bold
    end

    #Pie de pagina
    pdf.repeat(:all, :dynamic => true) do
      pdf.draw_text Time.now.strftime("%d-%m-%Y").to_s, :at => [0, 0]
      pdf.draw_text pdf.page_number, :at => [530, 0]
    end
    #Nombre de reporte
    send_data pdf.render, :filename => "Reporte de Traslados" << ".pdf", :type => "application/pdf"
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

end
